<?php
namespace app\api\controller;

use app\service\ai\DeepseekClient;
use app\service\DeepseekService;
use app\service\http\yzt\social\Social;
use support\Request;

class TestController
{
    public function test(Request $request)
    {

        try {


            $ai = new DeepseekClient();
            $res = $ai->chat("翻译成越南地址：越南 胡志明市 平新郡 新造访 53C25-27-29，只返回翻译为越南文其他东西不用返回");
            print_r($res);
            return $res;
            /*$service = new DeepseekService();

            // 获取动态数据
            $dynamics = $service->getDailyDynamics();

            // 安全检查，确保返回的是数组
            if (!is_array($dynamics)) {
                $dynamics = []; // 转换为空数组
            }

            return json(['code' => 0, 'msg' => 'success', 'data' => $dynamics]);*/

        } catch (\Exception $e) {
            // 记录错误信息
            error_log("API调用失败: " . $e->getMessage());

            return json([
                'code' => 500,
                'msg' => $e->getMessage(),
                'data' => []  // 确保返回空数组而不是null
            ]);
        }
    }

    public function news(Request $request)
    {
        try {
            // 从请求中获取国家代码和数量，如果没有则使用默认值
            $country = $request->get('country', 'CN');
            $count = (int)$request->get('count', 5);

            // 限制数量范围
            $count = min(max($count, 1), 10);

            // 初始化服务
            $apiKey = config('deepseek.api_key', 'sk-371fe28f28af4cf0bc8e071402601ddd');
            $service = new DeepseekService($apiKey);

            // 获取新闻数据
            $news = $service->getCountryNews($country, $count);

            return json(['code' => 0, 'msg' => 'success', 'data' => $news]);

        } catch (\Exception $e) {
            return json([
                'code' => 500,
                'msg' => $e->getMessage(),
                'data' => []
            ]);
        }
    }

    public function latest()
    {
        $exchangerate = new \app\service\http\exchangerate\Exchangerate();
        try {
            $data = $exchangerate->latest('CNY');
            return json(['code' => 0, 'msg' => 'success', 'data' => $data]);
        } catch (\Exception $e) {
            return json([
                'code' => 500,
                'msg' => $e->getMessage(),
                'data' => []
            ]);
        }

    }

    public function yztSocial()
    {

        /*$dynamic = Db::table('user_dynamics')
            ->where('publish_status', 0)  // 未发布
            ->where('review_status', 1)   // 审核通过
            ->whereNull('delete_time')    // 未删除
            ->inRandomOrder()->first();
        print_r($dynamic->content);*/

        // 获取用户信息
        //$user = UserModel::getOne(['id' => $dynamic->user_id]);

        /*$yztUserHttp = new \app\service\http\yzt\user\YztUser();
        $token = $yztUserHttp->login($user['account']);*/
        $social = new Social();
        $res = $social->send();

        return $res;

    }



}